/**
 * 格式化后端数据
 * @param res
 * @returns {*[]}
 */
export function formatData(res) {
  let data = []
  // 格式化后端的结构
  res.data.dateRangeVOList.forEach(item => {
    const date = [item.beginDate, item.endDate]// 处理时间段
    // 处理类型数组
    const arr = []
    let type = 1
    const dataArr = []
    if (item.buildingPointVOList.length > 0) {
      if (item.buildingPointVOList[0].comboCountInfoList) {
        type = 0
      }
    }
    item.buildingPointVOList.forEach(t => {
      let tmpComboList = []
      if (t.comboCountInfoList && t.comboCountInfoList.length > 0) {
        for (let i = 0; i < t.comboCountInfoList.length; i++) {
          let tmp = {}
          let sum = 0
          tmp.ruleList = []
          for (let j = 0; j < t.comboCountInfoList[i].ruleCountList.length; j++) {
            sum += t.comboCountInfoList[i].ruleCountList[j].count
            let tmp_rule = {
              building_area_id: t.id,
              combo_id: t.comboCountInfoList[i].comboId,
              rule_id: t.comboCountInfoList[i].ruleCountList[j].ruleId,
              name: String.fromCharCode(j + 65) + '组合(' + t.comboCountInfoList[i].ruleCountList[j].ruleName + ')',
              count: t.comboCountInfoList[i].ruleCountList[j].count
            }
            tmp.ruleList.push(tmp_rule)
          }
          tmp.id = t.comboCountInfoList[i].comboId
          tmp.combo_id = t.comboCountInfoList[i].comboId
          tmp.count = sum
          tmp.name = String.fromCharCode(65 + i) + '套餐'
          tmpComboList.push(tmp)
        }
      }
      t.comboCountInfoList = tmpComboList
      var keys = Object.keys(t.pointTypeCountVOMap)
      keys.forEach(i => {
        // 加入类型数据
        if (arr.find(tmp => tmp == i) == undefined) {
          arr.push(i)
        }
        t.pointTypeCountVOMap[i].to = t.pointTypeCountVOMap[i].point_type_count
        t.pointTypeCountVOMap[i].mediaPointList.forEach(tmp => {
          tmp.checked = true
        })
      })
      dataArr.push(Object.assign({}, t))
    })
    data.push(
      {
        type,
        date,
        arr,
        dataArr,
        relationPointArr: []
      }
    )
  })
  return data
}

/**
 * 格式化后端数据
 * @param res
 * @returns {*[]}
 */
export function formatDataToSale(res) {
  let data = []
  // 格式化后端的结构
  res.data.dateRangeVOList.forEach(item => {
    const date = [item.beginDate, item.endDate]// 处理时间段
    // 处理类型数组
    const arr = []
    let type = 1
    const dataArr = []
    if (item.buildingPointVOList.length > 0) {
      if (item.buildingPointVOList[0].comboCountInfoList) {
        type = 0
      }
    }
    item.buildingPointVOList.forEach(t => {
      let tmpComboList = []
      if (t.comboCountInfoList && t.comboCountInfoList.length > 0) {
        for (let i = 0; i < t.comboCountInfoList.length; i++) {
          let tmp = {}
          let sum = 0
          tmp.ruleList = []
          for (let j = 0; j < t.comboCountInfoList[i].ruleCountList.length; j++) {
            sum += t.comboCountInfoList[i].ruleCountList[j].count
            let tmp_rule = {
              building_area_id: t.id,
              combo_id: t.comboCountInfoList[i].comboId,
              rule_id: t.comboCountInfoList[i].ruleCountList[j].ruleId,
              name: String.fromCharCode(j + 65) + '组合(' + t.comboCountInfoList[i].ruleCountList[j].ruleName + ')',
              count: t.comboCountInfoList[i].ruleCountList[j].count
            }
            tmp.ruleList.push(tmp_rule)
          }
          tmp.id = t.comboCountInfoList[i].comboId
          tmp.combo_id = t.comboCountInfoList[i].comboId
          tmp.count = sum
          tmp.name = String.fromCharCode(65 + i) + '套餐'
          tmpComboList.push(tmp)
        }
      }
      t.comboCountInfoList = tmpComboList
      var keys = Object.keys(t.pointTypeCountVOMap)
      keys.forEach(i => {
        // 加入类型数据
        if (arr.find(tmp => tmp == i) == undefined) {
          arr.push(i)
        }
        t.pointTypeCountVOMap[i].to = t.pointTypeCountVOMap[i].point_type_count
        t.pointTypeCountVOMap[i].mediaPointList.forEach(tmp => {
          tmp.checked = true
        })
      })
      if (t.workers == null || t.workers.length == 0) {
        dataArr.push(Object.assign({ works: [] }, t))
      } else {
        dataArr.push(Object.assign({ works: t.workers }, t))
      }
    })
    data.push(
      {
        type,
        date,
        arr,
        dataArr
      }
    )
  })
  return data
}

/**
 * 获取配置项的楼盘数据
 * @param data
 * @returns {*[]}
 */
export function getBuildingWorkers(data) {
  const arr = []
  data.forEach(config => {
    config.dataArr.forEach(item => {
      if (item.works.length > 0) {
        arr.push({
          id: item.id,
          works: item.works.map(i => {
            return i
          })
        })
      }
    })
  })
  return arr
}

/**
 * 格式化请求对象
 * @param data
 * @param index
 */
export function formatRequest(data, index) {
  const newData = {}
  newData.beginDate = data.date[0]
  newData.endDate = data.date[1]
  newData.pointList = []
  newData.comboInfoList = []
  newData.index = index
  for (let i = 0; i < data.dataArr.length; i++) {
    data.arr.forEach(item => {
      if (data.dataArr[i].pointTypeCountVOMap[item]) {
        data.dataArr[i].pointTypeCountVOMap[item].mediaPointList.forEach(
          i => {
            if (i.checked) {
              let assign = Object.assign({}, i)
              assign.relation_point = undefined
              newData.pointList.push(assign)
            }
          }
        )
      }
    })
    if (data.type == 0) {
      data.dataArr[i].comboCountInfoList.forEach(combo => {
        combo.ruleList.forEach(rule => {
          newData.comboInfoList.push(Object.assign({}, rule))
        })
      })
    }
  }
  return newData
}

/**
 * 格式化错误数据
 * @param errorData
 * @param data
 * @param pointArr,
 * @param vc
 */
export function formatErrorData(data, errorData, pointArr, vc) {
  let index = errorData.index
  let type = (data.tmpDataList[index] || {}).type
  errorData.repeatMediaPointList.forEach(item => {
    let count = 0
    let arr = []// 第二个遍历每层的数据
    Object.keys(item.media_point_list_map).forEach(i => {
        count += item.media_point_list_map[i].length
        // 整理数组数据
        arr.push({
          id: 'type' + i,
          name: (pointArr.find(p => p.id == i) || {}).name + '(' + item.media_point_list_map[i].length + ')',
          points: item.media_point_list_map[i]
        })
        data.tmpDataList[index].dataArr.forEach(tmp => {
          if (item.id == tmp.id) {
            item.media_point_list_map[i].forEach(point => {
              // 加入detail属性
              let str = ''
              if (i == 9) {//综合市场类型
                str = point.ab_type_id == 57 ? 'A面' : 'B面'
              } else {
                str = (point.building_number != null ? point.building_number + '号楼' : '') +
                  (point.unit_number != null ? point.unit_number + '单元' : '') +
                  (point.floor != null ? point.floor + '层' : '') + point.code
              }
              vc.$set(point, 'detail', str)
              // 去掉可选中的数据
              if (errorData.type == 2 && type == 1) {
                tmp.pointTypeCountVOMap[i].to--
                tmp.pointTypeCountVOMap[i].mediaPointList.forEach(p => {
                  if (p.id == point.id) {
                    p.checked = false
                  }
                })
              }
            })
          }
        })
      }
    )
    // 加上楼盘统计
    vc.$set(item, 'count', count)
    // 加入点位数据
    vc.$set(item, 'pointTypeArrData', arr)
  })
}
